import { createRouter, createWebHashHistory } from "vue-router";
// 首页
import home from "@/layout/home.vue";

// 路由列表
const routes = [
    {
        path: "/", // 首页
        name: "控制台",
        component: home,
        redirect: "/dashboard/analysis",
        children: [
            {
                path: "dashboard/analysis",
                name: "分析页",
                component: () => import("@/views/dashboard/analysis.vue"),
            },
            {
                path: "dashboard/workbench",
                name: "工作台",
                component: () => import("@/views/dashboard/workbench.vue"),
            },
        ]
    },
    {
        path: "/",
        name: "系统管理",
        component: home,
        children: [
            {
                path: "system/account",
                name: "用户管理",
                component: () => import("@/views/system/account.vue"),
            },
            {
                path: "system/role",
                name: "角色管理",
                component: () => import("@/views/system/role.vue"),
            },
            {
                path: "system/shop",
                name: "商品管理",
                component: () => import("@/views/system/shop.vue"),
            },
            {
                path: "system/order",
                name: "订单管理",
                component: () => import("@/views/system/order.vue"),
            },
        ]
    },
    {
        path: "/",
        name: "日志管理",
        component: home,
        children: [
            {
                path: "log/login_log",
                name: "登录日志",
                component: () => import("@/views/log/login_log.vue"),
            },
        ]
    },
    {
        path: "/",
        name: "个人中心",
        component: home,
        children: [
            {
                path: "individual/profile",
                name: "个人资料",
                component: () => import("@/views/individual/profile.vue"),
            },
        ]
    },
    {
        path: "/",
        name: "会员管理",
        component: home,
        children: [
            {
                path: "member/memberlevel",
                name: "会员等级",
                component: () => import("@/views/member/memberlevel.vue"),
            },
            {
                path: "member/member",
                name: "会员管理",
                component: () => import("@/views/member/member.vue"),
            },
        ]
    },
    {
        path: "/",
        name: "系统设置",
        component: home,
        children: [
            {
                path: "setting/monitor",
                name: "服务监控",
                component: () => import("@/views/setting/monitor.vue"),
            },
        ]
    },
    {
        path: "/login",
        name: "Login",
        component: () => import("@/views/login/login.vue"),
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 路由守卫
const whiteList = ['/login'];
router.beforeEach((to, from, next) => {
    const token = JSON.parse(localStorage.getItem("CJia520のAdminUser"));
    if (token) {
        if (to.path === "/login") {
            next('/');
        } else {
            next();
        }
    } else {
        if (whiteList.includes(to.path)) {
            next();
        } else {
            next('/login');
        }
    }
});

// 导出路由
export default router;